package com.ningxia.water.service.impl;

import com.ningxia.water.entity.TsApplicantWell;
import com.ningxia.water.entity.TsWellInfo;
import com.ningxia.water.enums.WellZoneTypeEnum;
import com.ningxia.water.mapper.TsApplicantWellMapper;
import com.ningxia.water.mapper.TsWellInfoMapper;
import com.ningxia.water.mapper.ZtAreaInfoMapper;
import com.ningxia.water.mapper.ZtVillageInfoMapper;
import com.ningxia.water.service.ITsWellInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ningxia.water.utils.checkFieldIsNullUtils;
import com.ningxia.water.vo.out.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.ListIterator;

/**
 * @author hz
 * @since 2021-03-15
 */
@Service
public class TsWellInfoServiceImpl extends ServiceImpl<TsWellInfoMapper, TsWellInfo> implements ITsWellInfoService {

    @Autowired
    private TsWellInfoMapper wellInfoMapper;

    @Autowired
    private ZtAreaInfoMapper areaInfoMapper;

    @Autowired
    private ZtVillageInfoMapper villageInfoMapper;

    @Autowired
    private TsApplicantWellMapper applicantWellMapper;

    @Override
    public WellStatusCountsVo getWellStatusCount() {
        return wellInfoMapper.getWellStatusCount();
    }

    @Override
    public List<RankConsumedVo> rankOfWaterConsumed(String startTime, String endTime) {
        return wellInfoMapper.rankOfWaterConsumed(startTime, endTime);
    }

    @Override
    public List<RankConsumedVo> getWaterConsumed(String startTime, String endTime) {
        return wellInfoMapper.getWaterConsumed(startTime, endTime);
    }

    @Override
    public List<MonthConsumedVo> getMonthConsumed(String startTime, String endTime) {

        List<MonthConsumedVo> list = wellInfoMapper.getMonthConsumed(startTime, endTime);
        ListIterator<MonthConsumedVo> iterator = list.listIterator();
        while (iterator.hasNext()) {
            MonthConsumedVo next = iterator.next();
            next.setMonth(next.getMonth().split("-")[1]);
        }
        return list;
    }


    @Override
    public Double getTotalConsumed(String startTime, String endTime) {
        return wellInfoMapper.getTotalConsumed(startTime, endTime);
    }


    @Override
    public List<WellInfoVo> getWellInfos() {
        List<WellInfoVo> wellInfos = wellInfoMapper.getWellInfos();

        ListIterator<WellInfoVo> it = wellInfos.listIterator();
        while (it.hasNext()) {
            WellInfoVo wiv = it.next();
            if (wiv.getZoneType() == WellZoneTypeEnum.AREA.getCode()) {
                wiv.setAddress(areaInfoMapper.getWellAddress(wiv.getZoneId()));
            } else {
                wiv.setAddress(villageInfoMapper.getWellAddress(wiv.getZoneId()));
            }
        }
        return wellInfos;
    }

    @Override
    public List<WellInfoVo> findWellInfosByConditions(String applicantName, String wellCode, String wellName, String address, Integer wellType, Integer status, Integer zoneType) {
        //条件查询
        List<WellInfoVo> wellInfos = wellInfoMapper.findWellInfosByConditions(applicantName, wellCode, wellName, wellType, status, zoneType);
        //如果有地址查询条件
        if (address != null) {
            ListIterator<WellInfoVo> it = wellInfos.listIterator();
            while (it.hasNext()) {
                WellInfoVo wi = it.next();
                if (!checkFieldIsNullUtils.checkFiledIsNotNull(wi, "zoneType")) {
                    it.remove();
                    continue;
                }
                if (wi != null && wi.getZoneType() == WellZoneTypeEnum.AREA.getCode()) {
                    //在工业园区模糊查询地址
                    String realAddress = areaInfoMapper.findWellAddressByLike(address, wi.getZoneId());
                    if (realAddress == null) {
                        it.remove();
                    } else {
                        wi.setAddress(realAddress);
                    }
                } else {
                    //在村庄模糊查询地址
                    String realAddress = villageInfoMapper.findWellAddressByLike(address, wi.getZoneId());
                    if (realAddress == null) {
                        it.remove();
                    } else {
                        wi.setAddress(realAddress);
                    }
                }
            }
        } else {
            ListIterator<WellInfoVo> it = wellInfos.listIterator();
            while (it.hasNext()) {
                WellInfoVo wiv = it.next();
                if (!checkFieldIsNullUtils.checkFiledIsNotNull(wiv, "zoneType")) {
                    it.remove();
                    continue;
                }
                if (wiv != null && wiv.getZoneType() == WellZoneTypeEnum.AREA.getCode()) {
                    wiv.setAddress(areaInfoMapper.getWellAddress(wiv.getZoneId()));
                } else {
                    wiv.setAddress(villageInfoMapper.getWellAddress(wiv.getZoneId()));
                }
            }
        }


        return wellInfos;
    }

    @Override
    public Integer updateStatus(Integer wellId, int status) {
        return wellInfoMapper.updateStatus(wellId, status);
    }

    @Override
    public List<ReportVo> getWellReport(String type, String currentTime) {
        /**
         * 根据传入类型type判断，返回年报、月报、日报
         */
        if (type.equals("year")) {
            // 年报
            return wellInfoMapper.getWellReportByYear(currentTime);
        } else if (type.equals("month")) {
            // 月报
            return wellInfoMapper.getWellReportByMonth(currentTime);
        } else if (type.equals("day")) {
            // 日报
            return wellInfoMapper.getWellReportByDay(currentTime);
        }
        return null;
    }

    /**
     * 添加机井信息
     *
     * @param wellInfo
     * @param applicantId
     * @return
     */
    @Override
    public boolean insertWellInfo(TsWellInfo wellInfo, Integer applicantId) {
        int f1 = wellInfoMapper.insert(wellInfo);
        Integer wellId = wellInfoMapper.selectByWellCode(wellInfo.getWellCode());
        int f2 = applicantWellMapper.insert(new TsApplicantWell(applicantId, wellId));
        return f1 > 0 && f2 > 0;
    }

    @Override
    public Integer findApplicantIdByWellId(Integer wellId) {
        return wellInfoMapper.findApplicantIdByWellId(wellId);
    }
}
